package com.fattyCorps.controller;

import com.fattyCorps.cache.PlayerCache;
import com.fattyCorps.obj.msg.HttpStatus;
import com.fattyCorps.obj.msg.SRet;
import com.fattyCorps.obj.msg.client.global.CCloseServerMsg;
import com.fattyCorps.service.GlobalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/gacha")
public class GlobalController {
    private final String CLOSE_SERVER_PASSWORD = "Hiwoernnzxg*$#!_jsw98546324";

    @Autowired
    GlobalService globalService;

    @Autowired
    PlayerCache playerCache;

    @PostMapping("closeServer")
    public SRet closeServer(HttpServletRequest request, @RequestBody CCloseServerMsg cCloseServerMsg) {
        if (cCloseServerMsg.getPwd().equals(CLOSE_SERVER_PASSWORD)) {
            //设置服务器状态为已关闭
            globalService.onServerClose();

            //保存内存数据到服务器
            playerCache.saveAll();
            return SRet.success();
        }

        return SRet.error(HttpStatus.CLOSE_SERVER_PWD_ERROR.getCode(), HttpStatus.CLOSE_SERVER_PWD_ERROR.getMsg());
    }
}
